<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者: 奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\controller;
use think\DB;
use think\facade\Request;
class FinanceWithdraw extends Base
{
  public function index(){
      $alias = '';        //需要别名时写别名，不需要的时候留空 
      $defaultWhere = '';  //默认where
      $defaultOrder = 'id desc,create_time desc';  //默认排序
    //接收搜索表单数据
      $SearchFormData = $this->getSearchFormData($defaultWhere,$defaultOrder);
      $where = $SearchFormData['where'];
      $order = $SearchFormData['order'];
      $pageNum = $SearchFormData['pageNum'];

//组建关键词数据
     $keywordsData  =[
                  [
                  'field'   =>'id', //字段名
                  'name'    =>'流水号', //文本显示名称
                  'alias'   => $alias,  //字段对应的别名
                  ],
                  [
                  'field'   =>'username',
                  'name'    =>'用户名',
                  'alias'   => $alias,
                  ],
                  [
                  'field'   =>'realname',
                  'name'    =>'姓名',
                  'alias'   => $alias,
                  ],
                  [
                  'field'   =>'card_number',
                  'name'    =>'卡号',
                  'alias'   => $alias,
                  ],
                 [
                  'field'   =>'editor',
                  'name'    =>'受理人',
                  'alias'   => $alias,
                  ]
     ];
//组建日期筛选   
       $dateData = [
                  [
                  'title'        =>'申请时间',  //标签显示名称
                  'field'        =>'create_time', //筛选字段
                  'start_name'   =>'开始时间', //开始时间
                  'end_name'     =>'结束时间', //结束时间
                  'alias'        => $alias,  //字段别名
                  ],

              ];
//单个字段筛选
    $customSingleField =[
                            [
                             'name'  =>'状态',
                             'field' => 'status', 
                             'data'  => [ 0 =>'待审核',1=>'成功',2=>'失败'],
                             'alias' =>$alias,
                            ]
                       ]; 
// 组件字段组筛选
    $groupData = [];
     $orderData   = [
                 [
                 'name'     =>'提现金额多到少',
                 'field'    =>'amount',  //字段名
                 'orderby'  =>'desc',     //注册排序
                 'alias'    => $alias,         //数据表别名
                 ],
                 [
                 'name'     =>'提现金额少到多',
                 'field'    =>'amount',
                 'orderby'  =>'asc',
                 'alias'    => $alias,
                 ],
                [
                 'name'     =>'实际到帐多到少',
                 'field'    =>'actual_account',  //字段名
                 'orderby'  =>'desc',     //注册排序
                 'alias'    => $alias,         //数据表别名
                 ],
                 [
                 'name'     =>'实际到帐少到多',
                 'field'    =>'actual_account',
                 'orderby'  =>'asc',
                 'alias'    => $alias,
                 ],
               ];  

     $this->assign('keywordsData',$keywordsData);
     $this->assign('customSingleField',$customSingleField);
     $this->assign('groupData',$groupData);
     $this->assign('dateData',$dateData);
     $this->assign('orderData',$orderData);

     $list = DB::name('financeWithdraw')->where($where)->order($order)->paginate(10);
     $page = $list->render();
     $this->assign('page', $page);
     $this->assign('list',$list->toArray()['data']);
     return $this->fetch();
 }
  public function del(){
    // 删除
    $param = $this->request->param();
    $id = $param['id'];
      if(empty($id)){
        return error_json('非法请求！');
      }

      if($this->delData('financeWithdraw','id')){  
        return success_json('删除成功');
      }else{
        return error_json('删除失败');
      }
  }

  public function audit(){
    if(!$this->request->isAjax()){ return error_json('非法请求');}
     $param = $this->request->param();
    if(!$param['id']){   return error_json('请求参数错误');}
    if(!$param['status']){  return error_json('请求参数错误');}
    // 需改状态前，需要检查下状态
         $id = $param['id'];
         $res = model('financeWithdraw')->getInfoById($id);
     if($res['status'] != 0){ return error_json('状态操作错误'); }

     $param['editor'] = session('admin_username'); //管理者
     if($param['status'] == 1){
 //提现成功
        //修改提现状态
   
        if(model('financeWithdraw')->updateStatus($param)){

  //保存成功记录到资金记录表中
          model('finance_record')->addRecord($res['uid'],-$res['amount'],"提现成功",'余额');
          return success_json('提现操作成功');
           }
     }elseif($param['status'] ==2){
// 提现失败
        //修改提现状态
        if(model('financeWithdraw')->updateStatus($param)){
          //退还用户金额
           model('user')->where("uid = '".$res['uid']."'")->setInc('money',$res['amount']);
          
           model('finance_record')->addRecord($res['uid'],$res['amount'],"提现失败资金返还",'余额');

        }



        return success_json('提现失败，资金返还用户');
     }
     

  
  }
}